package com.liuceng.zen.pojo.form;

import com.github.miemiedev.mybatis.paginator.domain.Order;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.liuceng.zen.Page;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * BaseForm.java
 * 
 * @author liyuhang
 */
@Data
public class BaseForm<T> implements Serializable {
  /** 默认页码 */
  private static final int DEFAULT_PAGE_NUMBER = 1;

  /** 默认每页记录数 */
  private static final int DEFAULT_PAGE_SIZE = 30;
  /** 最大页数 */
  private static final int DEFAULT_MAX_PAGE_SIZE = 1000;
  private Integer pageNumber;
  private Integer pageSize;
  protected Integer currentUserId;
  private PageBounds pageBounds;
  // 搜索条件
  public String searchProperty;
  // 搜索值
  public String searchValue;
  //代运营尺寸
  public Integer crtSize;
  //自运营尺寸
  public Integer crtSizeMediumMenuId;
  public Integer sitesetType;
  public Integer memberId;
  public Integer showType;//当前也看到的页面
  // 排序条件
  protected String orderProperty = "updated_at";
  // 排序方向
  protected String orderDirection = "desc";

  private List<T> pageList;
  private Page<T> page;
  private String sort;
  private List<Order> orders;
  private Map<String, String> orderExprs = new HashMap<String, String>();
  private Map<String, Object> filterMap = new HashMap<String, Object>();

  protected void addOrderExpr(String property, String expr) {
    this.orderExprs.put(property, expr);
  }

  public PageBounds toPageBounds() {
    List<Order> orders = Order.formString(sort);
    for (int i = 0; i < orders.size(); i++) {
      Order order = orders.get(i);
      if (orderExprs.get(order.getProperty()) != null) {
        orders.set(i, new Order(order.getProperty(), order.getDirection(),
            orderExprs.get(order.getProperty())));
      }
    }
    return new PageBounds(pageNumber, pageSize, orders);
  }

  public BaseForm() {
    this(DEFAULT_PAGE_NUMBER, DEFAULT_PAGE_SIZE, "updated_at", "desc");
  }


  public BaseForm(Integer pageNumber, Integer pageSize, String orderProperties, String order) {
    int curPageNumber = (pageNumber != null && pageNumber >= 1) ? pageNumber : DEFAULT_PAGE_NUMBER;
    int curPageSize = (pageSize != null && pageSize >= 1) ? pageSize : DEFAULT_PAGE_SIZE;
    String orderP = StringUtils.isNotBlank(orderProperties) ? orderProperties : "updated_at";
    order = StringUtils.isNoneBlank(order) ? order.trim().toLowerCase() : "";
    String orderB = "asc".equals(order.toLowerCase()) ? "asc" : "desc";
    List<Order> orders = Arrays.asList(Order.create(orderP, orderB));

    //
    this.pageBounds = new PageBounds(curPageNumber, curPageSize, orders);
  }

  /**
   * 获取页码
   * 
   * @return 页码
   */
  public int getPageNumber() {
    return pageBounds.getPage();
  }

  /**
   * 设置页码
   * 
   * @param pageNumber 页码
   */
  public void setPageNumber(int pageNumber) {
    if (pageNumber < 1) {
      pageNumber = DEFAULT_PAGE_NUMBER;
    }
    pageBounds.setPage(pageNumber);
  }

  /**
   * 获取每页记录数
   * 
   * @return 每页记录数
   */
  public int getPageSize() {
    return pageBounds.getLimit();
  }

  /**
   * 设置每页记录数
   * 
   * @param pageSize 每页记录数
   */
  public void setPageSize(int pageSize) {
    if (pageSize < 1) {
      pageSize = DEFAULT_PAGE_SIZE;
    }
    pageBounds.setLimit(pageSize);
  }

  /**
   * 设置最大访问页数
   * 
   * @param
   */
}
